---
title: "Cleaning religious conflict (RELAC) dataset"
---

Sources:
https://figshare.com/articles/dataset/Supplemental_Material_Relac-JCRrep_-_Disputes_over_the_Divine_Introducing_the_Religion_and_Armed_Conflict_RELAC_Data_1975_to_2015/7613681
https://files.webb.uu.se/uploader/1576/Codebook-RELAC.pdf

# Load

```{r}
# load packages
  source("helper-packages.R")

# load religious conflict dataset
  relac_raw <- 
    import("../raw-data/x-relac/Relac-JCRrep.xlsx")

# load cleaned nelda dataset
  nelda_raw <- 
    readRDS("../cleaned-data/x-1-nelda.rds")
```

# Clean 

```{r}
# clean
  relac_clean <- 
    relac_raw %>%
    clean_names() %>% 
    filter(rel_incomp == 1) %>% 
    distinct(location, start_date) %>% 
    separate(location, sep = ",", into = c("location_a", "location_b"))

# stack the international conflicts
  relac_stacked <- 
    relac_clean %>% 
    select(location = location_a, start_date) %>% 
    bind_rows(
      relac_clean %>% 
        select(location = location_b, start_date) %>% 
        filter(!is.na(location))) %>% 
    mutate(location = str_trim(location),
           conflict_start_date = as.Date(start_date),
           relac_country_common = countryname(location)) %>% 
    select(relac_country_common, conflict_start_date) %>% 
    arrange(relac_country_common, conflict_start_date) %>% 
    group_by(relac_country_common) %>% 
      mutate(order = 1:n()) %>% 
    ungroup() %>% 
    filter(order == 1) %>% 
    select(-order)
  
# get unique country/last-election list from nelda dataset, merge in conflict history, and create dummy indicator
  unique_elecs <- 
    nelda_raw %>% 
    select(
      resp_country_common = nelda_country_name,
      last_election = nelda_election_date) %>% 
    left_join(relac_stacked, by = c("resp_country_common" = "relac_country_common")) %>% 
    mutate(
      relac_previous_religious_conflict = 
        case_when(
          conflict_start_date < last_election ~ 1,
          TRUE ~ 0)) %>% 
    select(-conflict_start_date) %>% 
    arrange(resp_country_common, last_election)
```

# Save data

```{r}
  saveRDS(unique_elecs, "../cleaned-data/x-9-relac.rds")
```
